<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Controller;

/**
 * 首页接口
 */
class Index extends Controller
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index(){
        //$this->success('请求成功');
    }

    /**
     * 得到省份
     * 
     */
    public function province(){
        $ll = array();$llary = array();$returnarry = array();
        $ll = cache('province');
        if($ll==null){
            $ll = model('Province')->order('listorder','desc')->select();
            cache('province',$ll);           
        }
        
        if($this->request->get('currentid')){
            $sary = explode(',',$this->request->get('currentid'));
            for($i = 0;$i<count($ll);$i++){
                if(in_array($ll[$i]['itemid'], $sary)){
                    $ll[$i]['selected'] = 1;
                }else{
                    $ll[$i]['selected'] = 0;
                }
            }
        }
        print_r(json_encode($ll));exit;
        
        /*
        $sary = explode(',',$this->request->get('currentid'));
        foreach ($ll as $k => $v) {
            if(in_array($v['itemid'],$sary)){
                $ll[$k]['selected'] = 1;
            }else{
                $ll[$k]['selected'] = 0;
            }            
            $llary[$v['itemid']][] = $v;
        }
        print_r(json_encode($llary));exit;
        */
    }
    
    /**
     * 得到城市
     * @access public
     * @param int $pid 省份编号
     */
    public function city(){
        $ll = array();$ll2 = array();$province = array();$llary = array();
        $where = array();
        $provinceid = $this->request->post('pid');
        if($provinceid){
            $where[] = array('pid','eq',$provinceid);
            $ll = cache('province-'.$provinceid);
            if($ll==null){
                $ll = model('City')->where($where)->order('listorder','desc')->select();
                cache('province-'.$provinceid,$ll);
            }
        }else{
            $ll = model('City')->order('listorder','desc')->select();
        }
        
        $sary = explode(',',$this->request->get('currentid'));
        foreach ($ll as $k => $v) {
            if(in_array($v['itemid'],$sary)){
                $ll[$k]['selected'] = 1;
            }else{
                $ll[$k]['selected'] = 0;
            }            
            $llary[$v['itemid']] = $v;
        }
        print_r(json_encode($llary));exit;
    }
    /**
     * 得到县城
     * @access public
     * @param int $cid 城市编号
     */
    public function area(){
        $ll = array();$ll2 = array();$province = array();$llary = array();
        $cityid = $this->request->post('cityid');
        $ll = cache('city-'.$cityid);
        
        if($cityid){
            $where[] = array('cid','eq',$cityid);
            $ll = cache('city-'.$cityid);
            if($ll==null){
                $ll = model('Area')->where($where)->order('listorder','desc')->select();
                cache('city-'.$cityid,$ll);
            }
        }else{
            $ll = model('Area')->order('listorder','desc')->select();
        }
        $sary = explode(',',$this->request->get('currentid'));
        foreach ($ll as $k => $v) {
            if(in_array($v['itemid'],$sary)){
                $ll[$k]['selected'] = 1;
            }else{
                $ll[$k]['selected'] = 0;
            }            
            $llary[$v['itemid']] = $v;
        }
        print_r(json_encode($llary));exit;
    }
}
